﻿using System;
using System.Configuration;
using System.Net;
using TestDriver.Client.CatalogueServiceReference;
using TestDriver.Client.CustomerServiceReference;
using TestDriver.Client.DeliveryServiceReference;
using TestDriver.Client.OrderServiceReference;
using TestDriver.Client.StockServiceReference;
using TestDriver.Client.StoreServiceReference;
using TestDriver.Client.WarehouseServiceReference;
using DeliveryStatus = TestDriver.Client.DeliveryServiceReference.DeliveryStatus;

namespace TestDriver.Client
{
    static class Program
    {
        private static readonly Random random = new Random();

        static void Main(string[] args)
        {
            // **********************
            // ** CUSTOMER SERVICE **
            // **********************
            var customerProxy = new CustomerServiceClient();
            customerProxy.Open();
            CustomerDto customerDto = new CustomerDto();
            int r = GetRandomNumber();
            customerDto.Name = "Cust " + r;
            customerDto.Address = "Address " + r;
            var customerCreateResponse = customerProxy.CreateCustomer(customerDto);
            var customerGetResponse = customerProxy.GetCustomerById(customerCreateResponse.ResponseObject.Id);
            customerProxy.Close();

            // ***********************
            // ** CATALOGUE SERVICE **
            // ***********************
            var catalogueProxy = new CatalogueServiceClient();
            catalogueProxy.Open();
            CatalogueDto video1 = new CatalogueDto();
            r = GetRandomNumber();
            video1.CatNo = r.ToString();
            video1.Director = "Director " + r;
            video1.Genre = "Genre " + r;
            video1.Price = 10.17F;
            video1.Title = "Title " + r;
            var catalogueCreateResponse1 = catalogueProxy.CreateVideo(video1);
            CatalogueDto video2 = new CatalogueDto();
            r = GetRandomNumber();
            video2.CatNo = r.ToString();
            video2.Director = "Director " + r;
            video2.Genre = "Genre " + r;
            video2.Price = 22.21F;
            video2.Title = "Title " + r;
            var catalogueCreateResponse2 = catalogueProxy.CreateVideo(video2);
            var catalogueGetResponse1 = catalogueProxy.GetVideoById(catalogueCreateResponse1.ResponseObject.Id);
            var catalogueGetResponse2 = catalogueProxy.GetVideosByDirector(catalogueCreateResponse1.ResponseObject.Director);
            var catalogueGetResponse3 = catalogueProxy.GetVideosByGenre(catalogueCreateResponse1.ResponseObject.Genre);
            catalogueProxy.Close();

            // ***********************
            // ** WAREHOUSE SERVICE **
            // ***********************
            var warehouseProxy = new WarehouseServiceClient();
            warehouseProxy.Open();
            WarehouseDto warehouseDto = new WarehouseDto();
            r = GetRandomNumber();
            warehouseDto.Name = "Warehouse " + r;
            var createWarehouseResponse = warehouseProxy.CreateWarehouse(warehouseDto);
            var getWarehouseResponse = warehouseProxy.GetWarehouseById(createWarehouseResponse.ResponseObject.Id);
            warehouseProxy.Close();

            // *******************
            // ** STOCK SERVICE **
            // *******************
            var stockProxy = new StockServiceClient();
            stockProxy.Open();
            StockDto item = new StockDto();
            item.CatalogueId = catalogueGetResponse1.ResponseObject.Id;
            item.Holding = 15;
            item.WarehouseId = getWarehouseResponse.ResponseObject.Id;
            var createItemResponse1 = stockProxy.CreateItem(item);
            item = new StockDto();
            item.CatalogueId = catalogueGetResponse2.ResponseObject[0].Id;
            item.Holding = 10;
            item.WarehouseId = getWarehouseResponse.ResponseObject.Id;
            var createItemResponse2 = stockProxy.CreateItem(item);
            item = new StockDto();
            item.CatalogueId = catalogueGetResponse3.ResponseObject[0].Id;
            item.Holding = 5;
            item.WarehouseId = getWarehouseResponse.ResponseObject.Id;
            var createItemResponse3 = stockProxy.CreateItem(item);
            var getItemResponse1 = stockProxy.GetItemById(createItemResponse1.ResponseObject.Id);
            stockProxy.Close();

            // *******************
            // ** STORE SERVICE **
            // *******************
            var storeProxy = new StoreServiceClient();
            storeProxy.Open();
            StoreDto storeDto = new StoreDto();
            r = GetRandomNumber();
            storeDto.Name = "Store " + r;
            var createStoreResponse = storeProxy.CreateStore(storeDto);
            var getStoreResponse = storeProxy.GetStoreById(createStoreResponse.ResponseObject.Id);
            storeProxy.Close();

            // *******************
            // ** ORDER SERVICE **
            // *******************
            var orderProxy = new OrderServiceClient();
            orderProxy.Open();
            OrderDto orderDto = new OrderDto();
            Guid orderId = Guid.NewGuid();
            orderDto.Id = orderId;
            //orderDto.Total = 99.98F; -> set a DAL level as it depends on the catalogue price
            orderDto.OrderDate = DateTime.Now;
            orderDto.OrderStatus = OrderStatus.Submitted;
            orderDto.CustomerId = customerGetResponse.ResponseObject.Id;
            orderDto.StoreId = getStoreResponse.ResponseObject.Id;
            orderDto.WarehouseId = getWarehouseResponse.ResponseObject.Id;
            orderDto.OrderLinesDto = new OrderLinesDto[2];
            orderDto.OrderLinesDto[0] = new OrderLinesDto();
            orderDto.OrderLinesDto[0].OrderId = orderId;
            orderDto.OrderLinesDto[0].CatalogueId = catalogueGetResponse1.ResponseObject.Id;
            orderDto.OrderLinesDto[0].Quantity = 1;
            orderDto.OrderLinesDto[1] = new OrderLinesDto();
            orderDto.OrderLinesDto[1].OrderId = orderId;
            orderDto.OrderLinesDto[1].CatalogueId = catalogueGetResponse2.ResponseObject[0].Id;
            orderDto.OrderLinesDto[1].Quantity = 5;
            var createOrderResponse = orderProxy.CreateOrder(orderDto);
            var getOrderResponse = orderProxy.GetOrderById(createOrderResponse.ResponseObject.Id);
            orderProxy.Close();

            // **********************
            // ** DELIVERY SERVICE **
            // **********************
            var deliveryProxy = new DeliveryServiceClient();
            deliveryProxy.Open();
            DeliveryDto deliveryDto = new DeliveryDto();
            Guid deliveryId = Guid.NewGuid();
            deliveryDto.Id = deliveryId;
            deliveryDto.DeliveryDate = DateTime.Now;
            deliveryDto.Status = DeliveryStatus.Pending;
            deliveryDto.OrderId = getOrderResponse.ResponseObject.Id;
            deliveryDto.WareHouseId = getOrderResponse.ResponseObject.WarehouseId;
            deliveryProxy.CreateDelivery(deliveryDto, GetDeliveryStatusQueueName());
            Console.WriteLine("Delivery created. Id: {0}, Status: {1}", deliveryDto.Id, deliveryDto.Status);
            deliveryProxy.Close();

            Console.WriteLine();
            Console.WriteLine("All services have been invoked. This is the end of the Test Driver.");
            Console.ReadKey();
        }

        private static int GetRandomNumber()
        {
            int r = random.Next(1000);
            return r;
        }

        private static string GetDeliveryStatusQueueName()
        {
            string hostName = Dns.GetHostName();
            string queueName = ConfigurationManager.AppSettings["statusQueueName"];
            return "net.msmq://" + hostName + queueName;
        }
    }
}